package com.ifd.iview.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zk.ui.util.Initiator;

import com.ifd.iview.utils.IviewUtils;

public class AuthorizedZkPage extends GenericForwardComposer implements Initiator, Composer {

	@Override
	public void doInit(Page page, Map<String, Object> args) throws Exception {
		Subject user = SecurityUtils.getSubject();
		if(!user.isAuthenticated()){
/*			Map<String, Object> info = new HashMap<String, Object>();
			info.put("parent", page);
  			Executions.createComponents("/pages/user/login.zul", null, info);*/
			Execution exec = Executions.getCurrent();
			HttpServletRequest request = (HttpServletRequest)exec.getNativeRequest();
			Session session = user.getSession();
			String reqURI = request.getRequestURI();
			session.setAttribute("requestURI", reqURI);
			String uri = IviewUtils.getResourceURI("/pages/user/login.zul");
			IviewUtils.sendRedirect(uri);
		}	
	}
}
